/**
 * The MIT License	
 * 
 * Copyright (c) 2009 Ghost Interactive
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 * ghostinteractive@gmail.com
 * http://ghostinteractive.net/blog
 * @author Jeremy brown
 */
package net.ghostinteractive.overlays
{
	import flash.events.*;
	import flash.geom.Point;

	public class ProximityGate extends EventDispatcher
	{
		// -- Constants
		public static const OPENED:String 	= 'opened';
		public static const CLOSED:String 	= 'closed';
		
		// -- Protected
		/**
		 * A number that defines the minimum of the zoom range
		 */
		protected var _zmin		:Number;
		
		/**
		 * A number that defines the maximum of the zoom range
		 */
		protected var _zmax		:Number;
		
		// -- Read Only
		/**
		 * The current state of this gate object
		 */
		protected var _state:String;
		public function get state():String { return _state; }
		
		/**
		 * A point object representing the epicenter of this proximity 
		 * range relative to the original size of the source image
		 */
		protected var _position	:Point;
		public function get position():Point { return _position; }
		
		/**
		 * Constructor
		 * An abstract class for evaluating the proximity of a given point 
		 * relative to the original size of the source image
		 */
		public function ProximityGate()
		{
			_state = CLOSED;
		}
		
		// -----------------------------------------------------
		//
		//	PUBLIC
		//	
		// -----------------------------------------------------
		
		/**
		 * Abstract update method
		 * Intended to be overridden by sub classes
		 * @param center The coordinates of the image currently at the center of the MultiScaleImage
		 * @param zoom   The current zoom depth of the MultiScaleImage
		 */
		public function update( center:Point, zoom:Number ):void
		{
			// override w/ sub-class
		}
		
		// -----------------------------------------------------
		//
		//	PROTECTED
		//	
		// -----------------------------------------------------
		
		/**
		 * Set the state of the gate object
		 * Ensure that event is dispatched only if the new value 
		 * is different from the old value and also that it is valid
		 */
		protected function setState( value:String ):void
		{
			if( _state != value && validateState( value ) ) {
				_state = value;
				var type:String = _state == OPENED ? Event.OPEN : Event.CLOSE;
				dispatchEvent( new Event( type ) );
			}
		}
		
		/**
		 * State validation
		 */
		protected function validateState( newState:String ):Boolean
		{
			return newState == CLOSED ||
				   newState == OPENED;
		}
		
		/**
		 * Zoooooom range validation
		 */
		protected function zoomIsInRange( value:Number ):Boolean { return value >= _zmin && value <= _zmax; }
		
	}
}

	